//
//  SecondTableViewCell.m
//  Homework_19
//
//  Created by 徐昊 on 16/8/10.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "SecondTableViewCell.h"
#import "SecondModel.h"
#import "SecondCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@interface SecondTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *modelArray;
@end

@implementation SecondTableViewCell

- (void)layoutSubviews {
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentSize = CGSizeMake(self.contentView.frame.size.width, 0);
    _layout.itemSize = CGSizeMake(_collectionView.contentSize.width / 3, CGRectGetHeight(self.bounds));
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    [self handleData];
}
- (void)handleData {
    NSURLSession *urlSession2 = [NSURLSession sharedSession];
    NSURL *imageURL = [NSURL URLWithString:@"http://223.99.255.20/adnewnc.app.autohome.com.cn/autov5.7.0/ad/infoad.ashx?version=6.2.0&platform=2&appid=2&networkid=0&adtype=1&provinceid=210000&cityid=0&lng=121.550746&lat=38.889724&gps_city=210200&pageid=dd91cf8b-0a21-42f9-8a30-a958d350bd93&isretry=0&deviceid=867601022220526%20HTTP/1.1"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession2 dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSError *error = nil;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
//            NSLog(@"%@", dic);
            NSDictionary *result = [dic objectForKey:@"result"];
            _array = [NSArray array];
            _array = [result objectForKey:@"list"];
            _modelArray = [NSMutableArray array];
            for (NSDictionary *dic in _array) {
                SecondModel *model = [SecondModel secondWithDictionary:dic];
                [_modelArray addObject:model];
//                NSLog(@"%ld", _modelArray.count);
            }
            
            [_collectionView reloadData];
        });
    }];
    // 让 dataTask 执行
    [dataTask resume];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondReuse" forIndexPath:indexPath];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[_modelArray[indexPath.row] img]]];
    cell.label.text = [_modelArray[indexPath.row] seriesname];
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
